package com.tanhua.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tanhua.domain.AnalysisByDay;
import com.tanhua.domain.Year;
import com.tanhua.domain.YearVo;
import com.tanhua.mapper.AnalysisByDayMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;

import java.util.Date;
import java.util.List;

@Service
public class DateCountService {
    @Autowired
    private AnalysisByDayMapper analysisByDayMapper;
    //查询新增、活跃用户、次日留存率
    public ResponseEntity findCount(Long sd, Long ed, int type) throws ParseException {
          List<Year> thisYeasLiat=new ArrayList<>();
          List<Year> YeasLiat=new ArrayList<>();
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");

        Date star=new Date(sd);
        Date end=new Date(ed);
        Calendar c=Calendar.getInstance();
        c.setTime(star);
        c.add(Calendar.YEAR,-1);
        Date startime =  c.getTime();
        Calendar c1=Calendar.getInstance();
        c1.setTime(end);
        c1.add(Calendar.YEAR,-1);
        Date endtime = c1.getTime();
        QueryWrapper<AnalysisByDay> qw=new QueryWrapper<>();

        qw.between("record_date",star,end);
        List<AnalysisByDay> thislist=analysisByDayMapper.selectList(qw);

        QueryWrapper<AnalysisByDay> qw1=new QueryWrapper<>();

        qw1.between("record_date",startime,endtime);
        List<AnalysisByDay> lastlist=analysisByDayMapper.selectList(qw1);
        if(type==101){
           //101 新增
          if(thislist!=null){
              for (AnalysisByDay analysisByDay : thislist) {
                  System.out.println("新增："+analysisByDay);
                  Year year=new Year();
                  year.setAmount(analysisByDay.getNumRegistered());
                  year.setTitle(sdf.format(analysisByDay.getRecordDate()));
                  thisYeasLiat.add(year);
              }
          }
            if(lastlist!=null){
                for (AnalysisByDay analysisByDay : lastlist) {
                    Year year=new Year();
                    year.setAmount(analysisByDay.getNumRegistered());
                    year.setTitle(sdf.format(analysisByDay.getRecordDate()));
                    YeasLiat.add(year);
                }
            }
        }else if(type==102){
           //102 活跃用户
            if(thislist!=null){
                for (AnalysisByDay analysisByDay : thislist) {
                    Year year=new Year();
                    year.setAmount(analysisByDay.getNumActive());
                    year.setTitle(sdf.format(analysisByDay.getRecordDate()));
                    thisYeasLiat.add(year);
                }
            }
            if(lastlist!=null){
                for (AnalysisByDay analysisByDay : lastlist) {
                    Year year=new Year();
                    year.setAmount(analysisByDay.getNumActive());
                    year.setTitle(sdf.format(analysisByDay.getRecordDate()));
                    YeasLiat.add(year);
                }
            }
        } else if(type==103){
           //103 次日留存率
            if(thislist!=null){
                for (AnalysisByDay analysisByDay : thislist) {
                    Year year=new Year();
                    Integer numActive=analysisByDay.getNumActive();
                    Calendar cd=Calendar.getInstance();
                    cd.setTime(analysisByDay.getRecordDate());
                    cd.add(Calendar.DATE,-1);
                    Date starDate =cd.getTime();
                     String stareee=sdf.format(starDate);
                    Date parse = sdf.parse(stareee);
                    System.out.println(starDate);
                    QueryWrapper<AnalysisByDay> qwd=new QueryWrapper<>();
                    qwd.eq("record_date",parse);
                    AnalysisByDay analysis=analysisByDayMapper.selectOne(qwd);
                    if(analysis!=null){
                        int a= (int)(analysisByDay.getNumRetention1d()*1.0/analysis.getNumRegistered()*100);
                        year.setAmount(a);
                    }else {
                        year.setAmount(analysisByDay.getNumRetention1d()*100);
                    }

                    year.setTitle(sdf.format(analysisByDay.getRecordDate()));
                    thisYeasLiat.add(year);
                }
            }
            if(lastlist!=null){
                for (AnalysisByDay analysisByDay : lastlist) {
                    Year year=new Year();
                    Integer numActive=analysisByDay.getNumActive();
                    Calendar cd=Calendar.getInstance();
                    cd.setTime(analysisByDay.getRecordDate());
                    cd.add(Calendar.DAY_OF_YEAR,-1);
                    Date starDate =cd.getTime();
                    String format=sdf.format(starDate);
                    Date date=sdf.parse(format);
                    QueryWrapper<AnalysisByDay> qwd=new QueryWrapper<>();
                    qwd.eq("record_date",date);
                    //year.setAmount(analysisByDay.getNumRetention1d());
                    AnalysisByDay analysis=analysisByDayMapper.selectOne(qwd);
                    if(analysis.getNumRegistered()!=0){
                        int a= (int)(analysisByDay.getNumRetention1d()*1.0/analysis.getNumRegistered()*100);
                        year.setAmount(a);
                    }else{
                        int a= (int)(analysisByDay.getNumRetention1d()*1.0/100*100);
                        year.setAmount(a);
                    }

                    year.setTitle(sdf.format(analysisByDay.getRecordDate()));
                    YeasLiat.add(year);
                }
            }
       }
       YearVo yearVo=new YearVo();
        yearVo.setThisYear(thisYeasLiat);
        yearVo.setLastYear(YeasLiat);
      return ResponseEntity.ok(yearVo);
    }
}
